package com.huanuo.internetofthings.service.fastPeriphery;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.unicorn.framework.base.base.AbstractService;
import org.unicorn.framework.core.ResponseDto;
import org.unicorn.framework.core.SysCode;
import org.unicorn.framework.core.exception.PendingException;
import org.unicorn.framework.util.map.CoordinateRange;
import org.unicorn.framework.util.map.LngLat;
import org.unicorn.framework.util.map.MapsUtil;

import com.google.common.collect.Lists;
import com.huanuo.internetofthings.bo.fastPeriphery.FastPeriphery;
import com.huanuo.internetofthings.bo.impact.ImpactDeviceInfo;
import com.huanuo.internetofthings.dto.fastPeriphery.FastPeripheryRequestDto;
import com.huanuo.internetofthings.service.impact.ImpactDeviceInfoService;

/**
 * <p>
 * fast周边网络情况 服务实现类
 * </p>
 *
 * @author xiebin
 * @since 2018-07-25
 */
@Service
public class FastPeripheryFacadeService extends AbstractService {
	@Autowired
	private FastPeripheryService fastPeripheryService;
	@Autowired
	private ImpactDeviceInfoService impactDeviceInfoService;

	/**
	 * 根据中心点查询一定辐射范围内的基站列表
	 * @param fastPeripheryRequestDto
	 * @return
	 * @throws PendingException
	 */
	public ResponseDto<List<FastPeriphery>> getFastPeripheryList(FastPeripheryRequestDto fastPeripheryRequestDto)
			throws PendingException {
		if(StringUtils.isBlank(fastPeripheryRequestDto.getDeviceNo())){
			throw new PendingException(SysCode.SYS_FAIL, "设备ID不能为空");
		}
		if(fastPeripheryRequestDto.getDis()==null){
			fastPeripheryRequestDto.setDis(2d);
		}
		ImpactDeviceInfo impactDeviceInfo=impactDeviceInfoService.getImpactDeviceInfo(ImpactDeviceInfo.builder().deviceNo(fastPeripheryRequestDto.getDeviceNo()).build());
		if(impactDeviceInfo==null){
			throw new PendingException(SysCode.SYS_FAIL, "不存在该设备");
		}
		LngLat lngLat = new LngLat(impactDeviceInfo.getDeviceCurLongitude(),impactDeviceInfo.getDeviceCurLatitude());
		CoordinateRange coordinateRange = MapsUtil.getCoordinateRange(fastPeripheryRequestDto.getDis(), lngLat);
		List<FastPeriphery> list = fastPeripheryService.selectList(FastPeriphery.builder().coordinateRange(coordinateRange).build());
		List<FastPeriphery> fastPeripheryList=Lists.newArrayList();
		list.forEach(fastPeriphery->{
			double dis=MapsUtil.getDistance(lngLat.getLatitude(), lngLat.getLongitude(), fastPeriphery.getFastLatitude(), fastPeriphery.getFastLongitude());
		    System.out.println(dis);
			if(dis<=fastPeripheryRequestDto.getDis()*1000){
			  fastPeripheryList.add(fastPeriphery);
		  }
		});
		System.out.println(fastPeripheryList.size());
		return new ResponseDto<>(fastPeripheryList);

	}

}
